/**      
 * @file		formatting.cpp
 * @description		The application formats numbers from an input
 *			file called "numbers.txt" and outputs them into
 *			a file called "formatted.txt". The application
 *			utilizes io manipulator functions to format the 
 *			output in formatted.txt.
 * @course		CSCI 123 Section 00000
 * @assignment 
 * @date		mm/dd/yyyy
 * @author		Brad Rippe (00000000) brippe@fullcoll.edu
 * @version		1.0
 */
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

/* Declare the file names as global constants */
const char NUMBERS_FILE[]	= "numbers.txt";
const char OUTPUT_FILE[]	= "formatted.txt";

/**
 * Checks to determine if the file input stream was successfully opened
 * @param   	aFileStream the file stream to be checked
 * @pre     	aFileStream has been initialized
 * @post    	the application is terminated if the file stream was opened 
 *		correctly
 */
void checkFailure(ifstream& aFileStream);

/**
 * Checks to determine if the file input stream was successfully opened
 * @param   	aFileStream the file stream to be checked
 * @pre    	aFileStream has been initialized
 * @post    	the application is terminated if the file stream was opened 
 *		correctly
 */
void checkFailure(ofstream& aFileStream);

/**
 * @return zero if the application executes successfully
 */
int main() {
	ifstream inFile;	// we don't have to declare four
				// objects of type ifstream to read
				// from four different files
				// we can reuse one variable
	ofstream outFile;
	inFile.open(NUMBERS_FILE);
	checkFailure(inFile);
	outFile.open(OUTPUT_FILE);
	checkFailure(outFile);

	// format file output
	outFile.setf(ios::showpos);

	double number;
	int count = 1;
	// notice the inFile >> number as a boolean condition
	while(inFile >> number) {
		outFile << setw(10) << fixed
				//data to the right of the whitespaces
				<< showpoint << right 
				<< setprecision(2) << number;
		// don't show sign for every even number
		if((count % 2) == 0) {
			outFile << endl;
			outFile.unsetf(ios::showpos);
		} 
		// add the sign back for all numbers
		// devisable by 4
		if((count % 4) == 0) {
			outFile.setf(ios::showpos);
		}
		count++;
	}
	outFile.close();
	inFile.close();

	return 0;
}

void checkFailure(ifstream& aFileStream) {
	if(aFileStream.fail()) {
		cout << "Couldn't open file!\n";
		exit(1);
	}
}

void checkFailure(ofstream& aFileStream) {
	if(aFileStream.fail()) {
		cout << "Couldn't open file!\n";
		exit(1);
	}
}

